<?php
include_once("Data/MySQLDriver.php");
include_once("Application/Properties.php");

class MySQLDAO {
	private $databaseObject;
	private $databaseProperties;
	
	//constructor
	function MySQLDAO() {
		if($this->databaseObject == null) {
			$this->setConnection();
		}
	} 	
	
	// public methods 
	public function getRecordSetById($id,$paramBean=null) {
		$recordSet = array();
		$i = 0;
		try {
			$this->databaseObject->query($this->getSqlFromParamBean($id, $paramBean));
		} catch(Exception $e) {
			echo("There was a database error.<br><br>");
			die();
		}

		while($row = $this->databaseObject->fetch_row()) {
				 $recordSet = $this->loadBean($id,$row,$recordSet);
		}
		
		return $recordSet;		
	}
	public function executeChangeById($id,$paramBean=null) {
		try {		
			$this->databaseObject->query($this->getSqlFromParamBean($id, $paramBean));
		}
		catch (Exception $e) {
			echo "There was a database error.";
		}
	}
	
	// typical override methods for child DAOs
	protected function loadBean($row,$bean,$id) { }
	protected function getSqlById($id) { }
	protected function bindParameters($id,$paramBean) { }
	protected function getDbPropertiesFile($file="") {
		if($this->databaseProperties == null) {
			$dbConfigFile = fopen($file,"r+");
			$this->databaseProperties = new Properties();
			$this->databaseProperties->load($dbConfigFile);
		}
	}	

	// bind methods for binding sql paramters
	protected function bindString($string) { 
		return "'" . $string . "'"; 
	}
	protected function bindInt($int) { 
		return $int;	
	}
	protected function bindDate($date) { 
		return "'" . $date . "'";	
	}
	
	// private utility methods
	private function getSqlFromParamBean($id,$paramBean) {
		if($paramBean == null) {
			return $this->lookupSqlById($id);
		}
		else {
			return $this->parameterizeQuery($this->lookupSqlById($id),$this->bindParameters($id,$paramBean));
		}
		
	}
	private function parameterizeQuery($sql,$values) {
		$count = 1;
		$newSql = $sql;
		
		for($i=0;$i<count($values);$i++) {
			$newSql = substr_replace($newSql,$values[$i],strpos($newSql,"?"),1);
		}
		return $newSql;
	}

	// private database connectivity methods
	private function setConnection() {
		$DB = new MySQLDriver();
		$DB->obj['sql_database']     = $this->getDatabase();
		$DB->obj['sql_user']         = $this->getUsername();
		$DB->obj['sql_pass']         = $this->getPassword();
		$DB->obj['sql_host']         = 'localhost';
		
		$this->databaseObject = $DB;
		$this->databaseObject->connect();
	}
	private function getPassword() {
		$this->getDbPropertiesFile();
		return $this->databaseProperties->getProperty('database.password');
	}
	private function getUsername() {
		$this->getDbPropertiesFile();
		return $this->databaseProperties->getProperty('database.username');
	}
	private function getDatabase() {
		$this->getDbPropertiesFile();
		return $this->databaseProperties->getProperty('database.database');
	}
}
?>